
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DABL;


public partial class Control_EditQuestionnaire : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Request.QueryString["ID"] != null)
        {
            string QuestionnaireID = Convert.ToString(Request.QueryString["ID"]);
            Session["QuesionnaireID"] = QuestionnaireID;
            //show Quesionnaire title on the start of the page
            litQuestionnaire.Text = QuestionnaireController.QuestionnairesGetByID(QuestionnaireID).Tables[0].Rows[0]["QuestionnaireName"].ToString().Replace(Environment.NewLine,"<br>");
            if(!IsPostBack)
            {
                DataTable dt = new DataTable();
                dt = QuestionnaireController.QuestionsGetByQuestionnaireID(Convert.ToString(Session["QuesionnaireID"])).Tables[0];
                if(dt.Rows.Count == 0)
                {
                    /*** adding empty row so we can display the footer & header***/
                    addEmptyRow();

                }
                else
                {
                    gViewQuestions.DataSource = dt;
                    gViewQuestions.DataBind();

                }
                // select the question type for likertscale/open-ended/mcq
                ddlQuestionType.DataSource = QuestionnaireController.QuestionTypesGet();
                ddlQuestionType.DataTextField = "QuestionType";
                ddlQuestionType.DataValueField = "QuestionTypeID";
                ddlQuestionType.DataBind();
            }

        }

    }

 

    private void addEmptyRow()
    {
        Questionnaire dsQuestionnaire = new Questionnaire();
        Questionnaire.QST_QuestionsDataTable QuestionsDataTable = new Questionnaire.QST_QuestionsDataTable();
        // Remove contraints so an empty row can be added.
        QuestionsDataTable.Constraints.Clear();
        foreach(DataColumn dc in QuestionsDataTable.Columns)
            dc.AllowDBNull = true;
        // Add a blank row to the dataset
        QuestionsDataTable.Columns[0].AllowDBNull = true;
        QuestionsDataTable.Rows.Add(QuestionsDataTable.NewRow());
        // Bind the DataSet to the gViewQuestions
        gViewQuestions.DataSource = QuestionsDataTable;
        gViewQuestions.DataBind();
        // Get the number of columns to know what the Column Span should be
        int columnCount = gViewQuestions.Rows[0].Cells.Count;
        // Call the clear method to clear out any controls that you use in the columns.  I use a dropdown list in one of the column so this was necessary.
        gViewQuestions.Rows[0].Cells.Clear();
        gViewQuestions.Rows[0].Cells.Add(new TableCell());
        gViewQuestions.Rows[0].Cells[0].ColumnSpan = columnCount;


    }
    #region  gViewQuestions Event handlers
    protected void gViewQuestions_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        switch(e.CommandName)
        {
                // adding row in parent table
            case "Add":
                // Retrieve the row that contains the button clicked 
                // by the user from the Rows collection.
                GridViewRow row = (GridViewRow)((WebControl)e.CommandSource).Parent.Parent;
                Questionnaire dsQuestionnaire = new Questionnaire();
                Questionnaire.QST_QuestionsRow QuestionsRow = (Questionnaire.QST_QuestionsRow)dsQuestionnaire.QST_Questions.NewRow();
                QuestionsRow.Question = ((TextBox)row.FindControl("txtQuestion")).Text;
                QuestionsRow.QuestionnaireID_FK = new Guid(Convert.ToString(Session["QuesionnaireID"]));
                QuestionsRow.QuestionType_FK = Convert.ToInt16(ddlQuestionType.SelectedValue);
                QuestionsRow.UpperText = ((TextBox)row.FindControl("txtUpperText")).Text;
                QuestionsRow.LowerText = ((TextBox)row.FindControl("txtLowerText")).Text;
                TextBox txtQuestionOrder = (TextBox)row.FindControl("txtQuestionOrder");
                if(txtQuestionOrder.Text != "")
                {
                    QuestionsRow.QuestionOrder = Convert.ToInt32(txtQuestionOrder.Text);
                }
                QuestionnaireController.QuestionsAdd(QuestionsRow);
                gViewQuestions.DataSource = QuestionnaireController.QuestionsGetByQuestionnaireID(Convert.ToString(Session["QuesionnaireID"]));
                gViewQuestions.DataBind();
                gViewQuestions.Visible = true;
            
            break;
                /** expanding the child table***/
            case "Expand":

            // Convert the row index stored in the CommandArgument
            // property to an Integer.
            int index = Convert.ToInt32(e.CommandArgument);
            // Retrieve the row that contains the button clicked 
            // by the user from the Rows collection.
            row = gViewQuestions.Rows[index];

            PlaceHolder exp = (PlaceHolder)row.Cells[7].FindControl("Expanded");
            exp.Visible = !exp.Visible;
            LinkButton linkBtnAddOptions = (LinkButton)row.FindControl("linkBtnAddOptions");
            if(linkBtnAddOptions.Text == "Add Options")
                linkBtnAddOptions.Text = "Close";
            else
                linkBtnAddOptions.Text = "Add Options";
            GridView gViewOptions = (GridView)exp.FindControl("gViewOptions");
            DataKey myKey = gViewQuestions.DataKeys[row.RowIndex];
            dsQuestionnaire = new Questionnaire();
            //Questionnaire.OptionsRow objOptionsRow = (Questionnaire.OptionsRow)dsQuestionnaire.Options.NewRow();

            //Session["QuesionID"] = Convert.ToInt32(myKey["QuestionID"]);
            DataTable dtOption = QuestionnaireController.OptionsGetByQuestionID(Convert.ToInt32(myKey["QuestionID"])).Tables[0];
            if(dtOption.Rows.Count == 0)
            {
                // Remove contraints so an empty row can be added.
                dtOption.Constraints.Clear();
                foreach(DataColumn dc in dtOption.Columns)
                    dc.AllowDBNull = true;
                // Add a blank row to the dataset
                dtOption.Columns[0].AllowDBNull = true;
                dtOption.Rows.Add(dtOption.NewRow());
                // Bind the DataSet to the gViewQuestions
                gViewOptions.DataSource = dtOption;
                gViewOptions.DataBind();

                // Get the number of columns to know what the Column Span should be
                int columnCount = gViewOptions.Rows[0].Cells.Count;
                // Call the clear method to clear out any controls that you use in the columns.  I use a dropdown list in one of the column so this was necessary.
                gViewOptions.Rows[0].Cells.Clear();
                gViewOptions.Rows[0].Cells.Add(new TableCell());
                gViewOptions.Rows[0].Cells[0].ColumnSpan = columnCount;
            }
            else
            {
                gViewOptions.DataSource = dtOption;
                gViewOptions.DataBind();

            }
            break;
        }

    }
    protected void gViewQuestions_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            // Retrieve the LinkButton control from the first column.
            LinkButton linkBtnAddOptions = (LinkButton)e.Row.Cells[2].FindControl("linkBtnAddOptions");
            // Set the LinkButton's CommandArgument property with the
            // row's index.
            linkBtnAddOptions.CommandArgument = e.Row.RowIndex.ToString();
        }
        else if(e.Row.RowType == DataControlRowType.Footer)
        {
            /***only for likert scale ******/
            if(ddlQuestionType.SelectedValue != "2")
            {
                TextBox txtUpperText = ((TextBox)e.Row.FindControl("txtUpperText"));
                TextBox txtLowerText = ((TextBox)e.Row.FindControl("txtLowerText"));
                if(txtLowerText != null)
                    txtLowerText.Visible = false;
                if(txtUpperText != null)
                    txtUpperText.Visible = false;
                gViewQuestions.Columns[2].Visible = false;
                gViewQuestions.Columns[3].Visible = false;

            }
            else
            {
                gViewQuestions.Columns[2].Visible = true;
                gViewQuestions.Columns[3].Visible = true;

            }
        }
    }
    protected void gViewQuestions_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            DataKey myKey = gViewQuestions.DataKeys[e.Row.RowIndex];
            if(myKey["QuestionType_FK"].ToString() != "")
            {
                if(Convert.ToInt32(myKey["QuestionType_FK"]) == 3 || Convert.ToInt32(myKey["QuestionType_FK"]) == 4)// MCQ
                {
                    LinkButton linkBtnAddOptions = (LinkButton)e.Row.Cells[2].FindControl("linkBtnAddOptions");
                    linkBtnAddOptions.Visible = true;

                }
                else if(Convert.ToInt32(myKey["QuestionType_FK"]) == 2)//Liker Scale
                {
                    gViewQuestions.Columns[2].Visible = true;
                    gViewQuestions.Columns[3].Visible = true;
                }

            }

        }

    }
    protected void ddlQuestionType_SelectedIndexChanged(object sender, EventArgs e)
    {

        DataTable dtQuestions = QuestionnaireController.QuestionsGetByQuestionnaireID(Convert.ToString(Session["QuesionnaireID"])).Tables[0];
        if(dtQuestions.Rows.Count > 0)
        {
            gViewQuestions.DataSource = dtQuestions;
            gViewQuestions.DataBind();
            gViewQuestions.Visible = true;

        }
        else
        {
            Questionnaire dsQuestionnaire = new Questionnaire();
            Questionnaire.QST_QuestionsDataTable dt = new Questionnaire.QST_QuestionsDataTable();

            // Remove contraints so an empty row can be added.
            dt.Constraints.Clear();
            foreach(DataColumn dc in dt.Columns)
                dc.AllowDBNull = true;
            // Add a blank row to the dataset
            dt.Columns[0].AllowDBNull = true;
            dt.Rows.Add(dt.NewRow());
            // Bind the DataSet to the gViewQuestions
            gViewQuestions.DataSource = dt;
            gViewQuestions.DataBind();
            // Get the number of columns to know what the Column Span should be
            int columnCount = gViewQuestions.Rows[0].Cells.Count;
            // Call the clear method to clear out any controls that you use in the columns.  I use a dropdown list in one of the column so this was necessary.
            gViewQuestions.Rows[0].Cells.Clear();
            gViewQuestions.Rows[0].Cells.Add(new TableCell());
            gViewQuestions.Rows[0].Cells[0].ColumnSpan = columnCount;
        }

    }
    protected void gViewQuestions_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gViewQuestions.ShowFooter = false;
        gViewQuestions.SelectedIndex = -1;
        gViewQuestions.EditIndex = e.NewEditIndex;
        DataKey myKey = gViewQuestions.DataKeys[Convert.ToInt32(e.NewEditIndex)];

        gViewQuestions.DataSource = QuestionnaireController.QuestionsGetByQuestionnaireID(Convert.ToString(Session["QuesionnaireID"]));

        gViewQuestions.DataBind();

        if(Convert.ToInt32(myKey["QuestionType_FK"]) != 2)
        {
            gViewQuestions.Columns[2].Visible = false;
            gViewQuestions.Columns[3].Visible = false;  

        }
        else
        {
            gViewQuestions.Columns[2].Visible = true;
            gViewQuestions.Columns[3].Visible = true;

        }
    }
    protected void gViewQuestions_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        DataKey myKey = gViewQuestions.DataKeys[Convert.ToInt32(e.RowIndex)];
        QuestionnaireController.QuestionsDelete(Convert.ToInt32(myKey["QuestionID"]));
        gViewQuestions.DataSource = QuestionnaireController.QuestionsGetByQuestionnaireID(Convert.ToString(Session["QuesionnaireID"]));
        gViewQuestions.DataBind();

    }
    protected void gViewQuestions_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gViewQuestions.ShowFooter = true;
        gViewQuestions.EditIndex = -1;
        gViewQuestions.SelectedIndex = -1;
        gViewQuestions.DataSource = QuestionnaireController.QuestionsGetByQuestionnaireID(Convert.ToString(Session["QuesionnaireID"]));
        gViewQuestions.DataBind();

    }
    protected void gViewQuestions_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        DataKey myKey = gViewQuestions.DataKeys[Convert.ToInt32(e.RowIndex)];
        Questionnaire dsQuestionnaire = new Questionnaire();
        Questionnaire.QST_QuestionsRow QuestionsRow = (Questionnaire.QST_QuestionsRow)dsQuestionnaire.QST_Questions.NewRow();
        QuestionsRow.QuestionID = Convert.ToInt32(myKey["QuestionID"]);
        QuestionsRow.Question = ((TextBox)gViewQuestions.Rows[e.RowIndex].FindControl("edit_txtQuestion")).Text;
        TextBox txtQuestionOrder = (TextBox)gViewQuestions.Rows[e.RowIndex].FindControl("edit_txtQuestionOrder");
        if(txtQuestionOrder.Text != "")
        {
            QuestionsRow.QuestionOrder= Convert.ToInt32(txtQuestionOrder.Text);
        }
         
        QuestionsRow.QuestionnaireID_FK = new Guid(Convert.ToString( Session["QuesionnaireID"]));
        QuestionsRow.QuestionType_FK = Convert.ToInt32(myKey["QuestionType_FK"]);

        if(Convert.ToInt32(myKey["QuestionType_FK"]) == 2)
        {
            QuestionsRow.UpperText = ((TextBox)gViewQuestions.Rows[e.RowIndex].FindControl("edit_txtUpperText")).Text;
            QuestionsRow.LowerText = ((TextBox)gViewQuestions.Rows[e.RowIndex].FindControl("edit_txtLowerText")).Text;

        }
        else
        {
            QuestionsRow.UpperText = "";
            QuestionsRow.LowerText = "";
        }

        QuestionnaireController.QuestionsUpdate(QuestionsRow);
        gViewQuestions.ShowFooter = true;
        gViewQuestions.EditIndex = -1;
        gViewQuestions.DataSource = QuestionnaireController.QuestionsGetByQuestionnaireID(Convert.ToString(Session["QuesionnaireID"]));
        gViewQuestions.DataBind();
    }
    #endregion

    #region  gViewOptions Event handlers
    protected void gViewOptions_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        GridView gViewOptions = ((GridView)(sender));
        DataKey myKey = gViewOptions.DataKeys[e.RowIndex];
        TextBox txtQuestion = (TextBox)gViewOptions.Rows[e.RowIndex].FindControl("Edit_txtOptionText");
        Questionnaire dsQuestionnaire = new Questionnaire();
        Questionnaire.QST_OptionsRow OptionsRow = (Questionnaire.QST_OptionsRow)dsQuestionnaire.QST_Options.NewRow();
        OptionsRow.OptionText = txtQuestion.Text;
        OptionsRow.OptionID = Convert.ToInt32(myKey["OptionID"]);
        GridViewRow gViewQuestionsRow = (GridViewRow)gViewOptions.Parent.Parent.Parent;
        myKey = gViewQuestions.DataKeys[gViewQuestionsRow.RowIndex];
        OptionsRow.QuestionID_FK = Convert.ToInt32(myKey["QuestionID"]);
        QuestionnaireController.OptionsUpdate(OptionsRow);
        gViewOptions.ShowFooter = true;
        gViewOptions.EditIndex = -1;
        gViewOptions.DataSource = QuestionnaireController.OptionsGetByQuestionID(Convert.ToInt32(myKey["QuestionID"]));
        gViewOptions.DataBind();

    }
    protected void gViewOptions_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridView gViewOptions = ((GridView)(sender));
        DataKey myKey = gViewOptions.DataKeys[e.RowIndex];
        QuestionnaireController.OptionsDelete(Convert.ToInt32(myKey["OptionID"]));
        GridViewRow gViewQuestionsRow = (GridViewRow)gViewOptions.Parent.Parent.Parent;
        myKey = gViewQuestions.DataKeys[gViewQuestionsRow.RowIndex];
        gViewOptions.DataSource = QuestionnaireController.OptionsGetByQuestionID(Convert.ToInt32(myKey["QuestionID"]));
        gViewOptions.DataBind();
    }
    protected void gViewOptions_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView gViewOptions = ((GridView)(sender));
        gViewOptions.ShowFooter = true;
        gViewOptions.EditIndex = -1;
        gViewOptions.SelectedIndex = -1;
        GridViewRow gViewQuestionsRow = (GridViewRow)gViewOptions.Parent.Parent.Parent;
        DataKey myKey = gViewQuestions.DataKeys[gViewQuestionsRow.RowIndex];
        gViewOptions.DataSource = QuestionnaireController.OptionsGetByQuestionID(Convert.ToInt32(myKey["QuestionID"]));
        gViewOptions.DataBind();

    }
    protected void gViewOptions_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView gViewOptions = ((GridView)(sender));
        gViewOptions.ShowFooter = false;
        gViewOptions.SelectedIndex = -1;
        gViewOptions.EditIndex = e.NewEditIndex;
        GridViewRow gViewQuestionsRow = (GridViewRow)gViewOptions.Parent.Parent.Parent;
        DataKey myKey = gViewQuestions.DataKeys[gViewQuestionsRow.RowIndex];
        gViewOptions.DataSource = QuestionnaireController.OptionsGetByQuestionID(Convert.ToInt32(myKey["QuestionID"]));
        gViewOptions.DataBind();
    }
    protected void gViewOptions_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        switch(e.CommandName)
        {
            case "AddOptions":
            GridViewRow row = (GridViewRow)((WebControl)e.CommandSource).Parent.Parent;
            TextBox txtQuestion = (TextBox)row.FindControl("txtOptionText");
            Questionnaire dsQuestionnaire = new Questionnaire();
            Questionnaire.QST_OptionsRow OptionsRow = (Questionnaire.QST_OptionsRow)dsQuestionnaire.QST_Options.NewRow();
            OptionsRow.OptionText = txtQuestion.Text;
            GridView gViewOptions = ((GridView)(sender));
            GridViewRow gViewQuestionsRow = (GridViewRow)gViewOptions.Parent.Parent.Parent;
            DataKey myKey = gViewQuestions.DataKeys[gViewQuestionsRow.RowIndex];
            OptionsRow.QuestionID_FK = Convert.ToInt32(myKey["QuestionID"]);
            QuestionnaireController.OptionsAdd(OptionsRow);
            gViewOptions.DataSource = QuestionnaireController.OptionsGetByQuestionID(Convert.ToInt32(myKey["QuestionID"]));
            gViewOptions.DataBind();
            break;

        }

    }
    #endregion

}

